package com.xiaoluo.lottery.web;

import com.xiaoluo.lottery.common.enums.PayBuEnums;
import com.xiaoluo.lottery.common.weixin.base.DefaultPaidCallBack;
import com.xiaoluo.lottery.common.weixin.hb.beans.RedPackRes;
import com.xiaoluo.lottery.common.weixin.pay.beans.OrderNotifyRes;
import com.xiaoluo.lottery.entity.HbInfo;
import com.xiaoluo.lottery.entity.Order;
import com.xiaoluo.lottery.filter.HbFilter;
import com.xiaoluo.lottery.service.HbInfoServiceImpl;
import com.xiaoluo.lottery.service.WeiXinServiceImpl;
import com.xiaoluo.lottery.vo.DataPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import retrofit.converter.ConversionException;
import retrofit.converter.SimpleXMLConverter;
import retrofit.mime.TypedInput;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Project : lottery
 * Package : com.xiaoluo.lottery.web
 * Created by Willian.
 * Date  : 2016/4/6 10:14.
 *
 * 1、获取鉴权地址
 * 2、鉴权回调，获取accessToken
 * 3、统一下单，发起支付
 * 4、查询订单
 * 5、申请退款
 * 6、查询退款
 * 7、下载对账通知单
 * 8、支付结果通知
 * 9、测速上报
 * 10、push消息
 * 11、获取用户信息
 */
@Controller
@RequestMapping(value = "/pay")
public class PayController {
    @Autowired
    private WeiXinServiceImpl payServiceImpl;



    /**
     * 支付回调
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value="/notify")
    public void doNotify(ModelMap model,
                                   HttpServletRequest request,
                                   HttpServletResponse response){
        /**
        return payServiceImpl.paidCallBack(request);
        **/
        response.setContentType("application/xml; charset=UTF-8");
        try {
            OrderNotifyRes res = payServiceImpl.paidCallBack(request);
            response.getWriter().write(res.toXml());
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 统一下单
     * @param model
     * @param record
     * @return
     */
    @RequestMapping(value="/doPay")
    @ResponseBody
    public Order doPay(ModelMap model,
                        @RequestParam(value = "order", defaultValue = "") Order record){

        return payServiceImpl.doPay(record, PayBuEnums.LOTTERY,new DefaultPaidCallBack());
    }


}
